package com.wzh.store.controller;

import com.wzh.store.controller.ex.*;
import com.wzh.store.service.ex.*;
import com.wzh.store.utils.JsonResult;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpSession;

/**
 * @Author wzh
 * @create 2022/4/12 17:58
 * @Description： 控制层的基类，用于处理异常
 */
public class BaseController {
//    操作成功的状态码
    public static final int  ok=200;
    public static final String message="用户注册成功";
     /*
      *请求处理方法，这个方法的返回值就是需要传递给前端的参数
      * 自动将异常对象传递到此方法的参数列表上
      * 当前项目如果产生了异常，都被拦截到此方法中，这个方法此时就充当是请求处理方法，方法的返回值直接给到前端 */
    @ExceptionHandler({ServiceException.class,UpdateException.class, FileUploadException.class})//用于统一处理抛出的异常
    public JsonResult<Void> handleException(Throwable e){
        JsonResult<Void> jsonResult=new JsonResult<>(e);
        if(e instanceof UsernameDuplicatedException){
            jsonResult.setState(4000);
            jsonResult.setMessage(e.getMessage());
        }else if(e instanceof InsertException){
            jsonResult.setState(5000);
            jsonResult.setMessage(e.getMessage());
        }else if(e instanceof UserNotFoundException){
            jsonResult.setState(5001);
            jsonResult.setMessage(e.getMessage());
        }else if(e instanceof PasswordNotMatchException){
            jsonResult.setState(5002);
//            jsonResult.setMessage("密码错误，请重新输入");
            jsonResult.setMessage(e.getMessage());
        }else if (e instanceof UpdateException){
            jsonResult.setState(5003);
            jsonResult.setMessage(e.getMessage());
        }else if (e instanceof FileEmptyException){
            jsonResult.setState(6000);
            jsonResult.setMessage(e.getMessage());
        }else if (e instanceof FileStateEzxception){
            jsonResult.setState(6001);
            jsonResult.setMessage(e.getMessage());
        }else if (e instanceof FileTypeException){
            jsonResult.setState(6002);
            jsonResult.setMessage(e.getMessage());
        }else if (e instanceof FileUploadIoExceptkion){
            jsonResult.setState(6003);
            jsonResult.setMessage(e.getMessage());
        }else if(e instanceof AddressCountLimitException){
            jsonResult.setState(7001);
            jsonResult.setMessage(e.getMessage());
        }else if (e instanceof AccessDeniedException){
            jsonResult.setState(7002);
            jsonResult.setMessage(e.getMessage());
        }else if (e instanceof AddressNotFoundException){
            jsonResult.setState(7003);
            jsonResult.setMessage(e.getMessage());
        }else if (e instanceof ProductNotFindException){
            jsonResult.setState(7004);
            jsonResult.setMessage(e.getMessage());
        }else if (e instanceof CartNotFindException){
            jsonResult.setState(7005);
            jsonResult.setMessage(e.getMessage());
        }
        return jsonResult;
    }
    protected final Integer getUidFromSession(HttpSession session){
        return Integer.valueOf(session.getAttribute("uid").toString());
    }
//    protect修饰 只可在当前包下使用
    protected final String getUsernameFromSession(HttpSession session){
        return session.getAttribute("username").toString();
    }

}
